﻿;$(function() {
    //$("#toolbar").ligerToolBar({
    //    items: [{
    //        text: '添加',
    //        icon: '',
    //        id: 'btnPartitionSave',
    //        click: function (parameters) {
    //            location.href = "/Article/OpPartition";
    //        }
    //    }, {
    //        text: '删除',
    //        icon: '',
    //        id: 'btnPartitionDelete',
    //        click: function (parameters) {
    //            alert(1);
    //        }
    //    }]
    //});
    
    var manager = $("#partitionGrid").ligerGrid({
            columns: [
                {
                    display: '序号',
                    align: 'center',
                    width: 50,
                    minWidth: 50,
                    render: function (item, i) {
                        return (i + 1).toString();
                    }
                },
                { display: '编号', name: 'PartitionId', isSort: false, width: 50, align: 'left' },
                {
                    display: '类别名称',
                    align: 'left',
                    width: 200,
                    minWidth: 100,

                    render: function (item, i, value) {
                        var $wrap = $("<div />"),
                            $span = $("<span />"),
                            $text = $("<span />");

                        if (item.HasChildren == 1) {
                            $span.addClass("ifunny-grid-tree-link l-grid-tree-link-open")
                                .height(12)
                                .width(12);
                        }
                        $span.attr("data-item", JSON.stringify(item))
                            .css("margin-left", (item.Level * 10))
                            .html("&nbsp;&nbsp;&nbsp;");
                        $text.css("padding", "0 2px").text(item.Name);
                        if (item.Level == 0) {
                            $text.attr("title", item.PartitionId);
                        }

                        $wrap.append($span).append($text);
                        return $wrap.html();
                    }
                },
                { display: '类别描述', name: 'Description', isSort: false, width: 250, align: 'left' },
                { display: '是否启用', name: 'Enabled', isSort: false, width: 80, align: 'center',render: function(item,i,value) {
                    return item.Enabled ? "启用" : "禁用";
                }
                }
            ],
            width: '99.85%',
            height:
                '100%',
            heightDiff:
                -20,
            pageSizeOptions:
                [5, 10, 15, 20],
            //data: TreeDeptData,
            alternatingRow:
                false,
            url:
                "/Article/GridData",
            headerRowHeight:
                26,
            tree:
            {
                columnId: 'Name'
            },
            dataAction: "server",
            rowAttrRender:
                function (item, index) {
                    return "partitionid='" + item.PartitionId
                        + "' parentid='" + item.ParentId
                        + "' level='" + item.Level + "'";
                },

            onAfterShowData: function () {


                //#region 所有的复选框 并加上事件

                var $checkbox = $("#partitionGrid td.l-grid-row-cell input[type=checkbox]");

                $checkbox.each(function () {
                    var checked = this.checked;
                    var pid = $(this).data("partitionid");
                    $("#partitionGrid td.l-grid-row-cell input[data-partitionid^=" + pid + "]").each(function () {
                        this.checked = checked;
                        if (this.checked) {
                            $(this).prev(".l-checkbox").addClass("l-checkbox-checked");
                        } else {
                            $(this).prev(".l-checkbox").removeClass("l-checkbox-checked");
                        }
                    });
                    $(this).ligerCheckBox({ disabled: $(this).attr("disabled") == "disabled" });
                });

                //#endregion


                //#region 遍历所有 '展开/关闭' 图标 加上事件
                $("#partitionGrid .ifunny-grid-tree-link").click(function () {
                    var item = $(this).data("item");
                    if ($(this).hasClass("l-grid-tree-link-open")) {
                        $(this).removeClass("l-grid-tree-link-open").addClass("l-grid-tree-link-close");
                        $("#partitionGrid tr.l-grid-row[parentid^=" + item.PartitionId + "]").hide();

                    } else {
                        $(this).removeClass("l-grid-tree-link-close").addClass("l-grid-tree-link-open");
                        showChildren(item.PartitionId, item.Level + 1);
                    }
                });
                //#endregion

            }
        }
    );
});

//显示子节点

function showChildren(pid, i) {
    var $children = $("#partitionGrid tr.l-grid-row[parentid^=" + pid + "]").filter(function (index) {
        return $(this).attr("level") == i;
    });
    if ($children.lenth == 0) {
        return false;
    }
    $.each($children, function (i, v) {
        var $treeTrigger = $(this).find(".ifunny-grid-tree-link");
        if ($treeTrigger.length > 0) {
            if ($treeTrigger.hasClass("l-grid-tree-link-open")) {
                var item = $treeTrigger.data("item");
                showChildren(item.PartitionId, item.Level + 1);
            }
        }
        $(this).show();
    });
}